package hust.iamk.lock;

import java.io.File;
import java.io.FileNotFoundException;

import android.content.Context;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
import android.net.Uri;
import android.provider.MediaStore;

public class SingleMediaScanner implements MediaScannerConnectionClient {

	private MediaScannerConnection mMs;
	private File mFile;
	private Context mContext;

	public SingleMediaScanner(Context context, File f) {
		mFile = f;
		mMs = new MediaScannerConnection(context, this);
		mMs.connect();
	}

	@Override
	public void onMediaScannerConnected() {
		mMs.scanFile(mFile.getAbsolutePath(), "image/*");
	}

	@Override
	public void onScanCompleted(String path, Uri uri) {
		try {
			MediaStore.Images.Media.insertImage(mContext.getContentResolver(), path, SecurityFileUtils.getFileName(path, 0), "");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			mMs.disconnect();
		}
	}

}
